package cn.engage.user.controller;

import cn.engage.user.service.WxuserService;
import cn.engage.user.domain.Wxuser;
import cn.engage.user.query.WxuserQuery;
import cn.engage.basic.util.AjaxResult;
import cn.engage.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/wxuser")
public class WxuserController {

    @Autowired
    public WxuserService wxuserService;

    /**
     * 接口：添加或修改
     * @param wxuser  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Wxuser wxuser){
        try {
            if( wxuser.getId()!=null)
                wxuserService.update(wxuser);
            else
                wxuserService.add(wxuser);
            return new AjaxResult().me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().me().fail();
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            wxuserService.delete(id);
            return new AjaxResult().me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().me().fail();
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                wxuserService.patchDelete(ids);
            return new AjaxResult().me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().me().fail();
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Wxuser> findAll(){
        return wxuserService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Wxuser> queryByPage(@RequestBody WxuserQuery query) {
        return wxuserService.queryByPage(query);
    }
}
